﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Globalization;

using Bierdopje.RSS;
using Bierdopje.API;
using Bierdopje.API.Personal;
using AutoDownload.Lib;
using AutoDownload.Settings;

namespace TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-us");
            
            Bierdopje.API.HTTPHelper.UserAgent = "AutoDownloadSub TestConsole V" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
                
            Test _T = new Test();
            _T.LoadConfig();
            //_T.DownloadTest();
            _T.SubTest();

            //_T.CalendarTest();

            //Console.Read();
        }

        public class Test
        {
            public void CalendarTest()
            {
                ApiLibrary _API = new ApiLibrary(_Config.APIKey);
                ApiPersonal _PersonalApi = new ApiPersonal(@"", _API);

                Bierdopje.API.Personal.Calendar _Cal = _PersonalApi.getPersonalCalendar();

                foreach (CalendarDay _CalDay in _Cal.Days)
                {
                    Console.WriteLine(String.Format("Day {0}", _CalDay.Date.ToShortDateString()));
                    foreach (CalendarEpisode _CalEp in _CalDay.Episodes)
                    {
                        if (_CalEp.ApiShow == null)
                            Console.WriteLine(String.Format("\tShow {0} Episode {1} Age {2}", _CalEp.ShowName, _CalEp.EpisodeSEString, "API Couldn't find show"));
                        else
                            Console.WriteLine(String.Format("\tShow {0} Episode {1} ApiShow {2}", _CalEp.ShowName, _CalEp.EpisodeSEString, _CalEp.ApiShow.Name));
                    }
                }
            }

            public void DownloadTest()
            {
                ApiLibrary _API = new ApiLibrary(_Config.APIKey);

                ApiShow _Show = _API.GetShowById(12717);
                List<ApiEpisode> _Episodes = _Show.GetAllEpisodesForSeason(1);
                ApiEpisode _Episode = _Episodes[0];
                
                List<ApiSub> _Subs = _Episode.GetENGSubs();
                _Subs[0].Download(@"C:\temp\test.srt", true);
            }

            private Config _Config;
            public void SubTest()
            {
               Downloader _Downloader = new Downloader(Config.GetConfigFileName());
                _Downloader.Check();
            }

            public void LoadConfig()
            {
                _Config = Config.Load(Config.GetConfigFileName());
                _Config.DownoadedSubs.Clear();
                _Config.LastOverview = DateTime.MinValue;
                _Config.DownloadIfNoFilePresent = false;
                _Config.DownloadIfNoFilePresentMinAge = 2;
                _Config.Save(Config.GetConfigFileName());
            }
        }
    }
}
